package com.huawang.mdesign.model.service.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.huawang.business.util.ObjectMapperFactory;
import com.huawang.core.definition.mof.instances.MetaClassInstance;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @author baibf
 * @description 日志控制Service
 * @date 2024/6/27 9:31
 **/
@Slf4j
@Service
@Primary
public class LogUtil {

    private LogUtil() {
    }

    private static LoggerContext context = LoggerContext.getContext(false);
    private static LoggerConfig loggerConfig = context.getConfiguration().getRootLogger();

    public static String setLogLevel(String level) {
        String message = "success";
        switch (level) {
            case "trace":
                loggerConfig.setLevel(Level.TRACE);
                break;
            case "debug":
                loggerConfig.setLevel(Level.DEBUG);
                break;
            case "info":
                loggerConfig.setLevel(Level.INFO);
                break;
            case "warn":
                loggerConfig.setLevel(Level.WARN);
                break;
            case "error":
                loggerConfig.setLevel(Level.ERROR);
                break;
            default:
                message = "日志级别修改失败";
                break;
        }
        context.updateLoggers();
        return message;
    }


    /**
     * 上线排查问题日志打印
     *
     * @param message
     * @param infos
     * @return void
     * @author baibf
     * @date 2024/06/27 18:40
     */
    public static void printOnlineLogs(String message, Object... infos) {
        if (loggerConfig.getLevel() == Level.DEBUG) {
            log.info(message, infos);
        }
    }

    /**
     * 上线排查问题日志打印(序列化)
     *
     * @param messages
     * @param infos
     * @return void
     * @author baibf
     * @date 2024/06/27 18:40
     */
    public static void printSerializeOnlineLogs(String messages, Object... infos) {
        if (loggerConfig.getLevel() == Level.DEBUG) {
            Object[] result = new Object[infos.length];
            for (int i = 0; i < infos.length; i++) {
                final Object info = infos[i];
                if (Objects.isNull(info)) {
                    result[i] = null;
                } else if (info instanceof String) {
                    result[i] = info;
                } else if (info instanceof MetaClassInstance) {
                    result[i] = ((MetaClassInstance) info).getId();
                } else if (info instanceof Collection) {
                    final List<String> strList = new ArrayList<>(((Collection<?>) info).size());
                    ((Collection<?>) info).forEach(it -> {
                        if (it instanceof MetaClassInstance) {
                            strList.add(((MetaClassInstance) it).getId());
                        } else {
//                            strList.add(serializeInfo(it));
                        }
                    });
                    result[i] = strList;
                } else {
//                    result[i] = serializeInfo(info);
                }
            }
//            log.info(messages, result);
        }
    }


    public static String serializeInfo(Object object) {
        try {
            return ObjectMapperFactory.getMapper().writeValueAsString(object);
        } catch (JsonProcessingException e) {
            log.error("请求对象转换失败！", e);
        }
        return "";
    }
}
